// const App = {
//   data() {
//     return {
//       party_fees: {},
//       value: [], // 初始化 labelNumber 属性
//       Manager: [],
//       dyManager: [],
//       checkAll: false,
//       indeterminate: false
//     };
//   },
//   mounted() {
//     let TABLE_NAME = sessionStorage.getItem("TABLE_NAME");
//     console.log(TABLE_NAME);
//     this.Data = TABLE_NAME;
//     let type = sessionStorage.getItem("type");
//     console.log(type); 
//     //如果传过来的字符串是Edit调取Edit方法
//     if (type == "Edit") {
//       let key = sessionStorage.getItem("key");
//       console.log(key);
//       this.Edit(key);
//     }

//     this.Add()
//     console.log(this.checkAll);
//   },
//   watch: {
//     // 监听党组织变化
//     'party_fees.Party_Organization_SerialNumber'(newVal) {
//       if (newVal) {
//         this.party_fees.Party_Member_SerialNumber = []
//         this.checkAll = false
//         this.indeterminate = false
//        // this.loadDyManager(newVal)  假设这是加载方法
//       }
//     },
//     // 监听已选值变化
//     'party_fees.Party_Member_SerialNumber'(val) {
//        // 当没有可选项时，重置状态
//       if (this.dyManager.length === 0) {
//         this.checkAll = false;
//         this.indeterminate = false;
//         return;
//       }
//       // 当全部选中时
//       if (val.length === this.dyManager.length) {
//         this.checkAll = true
//         this.indeterminate = false
//         console.log(this.checkAll);

//       } 
//       // 当部分选中时
//       else if (val.length > 0) {
//         this.checkAll = false
//         this.indeterminate = true
//       } 
//       // 当未选中时
//       else {
//         this.checkAll = false
//         this.indeterminate = false
//       }
//     }
//   },
//   methods: {
//     //  全选/反选处理
//     handleCheckAll(val){
//       this.indeterminate =false
//       if (val) {
//         // 全选操作：提取所有选项的value
//         this.party_fees.Party_Member_SerialNumber = this.dyManager.map(
//           item => item.Party_Member_SerialNumber
//         )
//       } else {
//         // 取消全选
//         this.party_fees.Party_Member_SerialNumber = []
//       }
//     },
//     //选择党员流水号（党员编号）后，调用
//     handleTypeChange(selectedValue) {
//       console.log(selectedValue);
//       //把选中的获取的党组编号传入dyselect里
//       this.dyselect(selectedValue);
//       if(selectedValue){
//         this.party_fees.Party_Member_SerialNumber = ''
//       }
//     },
//     dyselect(selectedValue){
//       console.log(selectedValue);

//       //第二步 根据党组编号查党员编号
//       const _this = this;
//       $.ajax({
//         headers: {
//           "content-type": "application/x-www-form-urlencoded",
//         },
//         url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
//         method: "post",
//         data: {
//           Data: "party_member_information", //查询（党员流水号）党员编号的表名
//           Condition: " Party_Organization_SerialNumber = '" + selectedValue + "' ",
//         },
//         success: (res) => {
//           console.log("res", res);
//           _this.dyManager = res;
//         },
//         error: (err) => {
//           console.log(err); 
//         },
//       });
//     },
//     //添加进来的方法  筛掉删除的数据
//     Add() {
//       const _this = this;
//        // 第一步，先查党组表
//       $.ajax({
//         headers: {
//           "content-type": "application/x-www-form-urlencoded",
//         },
//         url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
//         method: "post",
//         data: {
//           Data: "party_organization", //查询党组的表名
//           Condition: " Row_Status='0'",
//         },
//         success: (res) => {
//           console.log("res", res);
//           //把查到值赋给Manager数组，然后绑定到下拉框，html页那边进行遍历后出现在下拉中
//           _this.Manager = res;
//         },
//         error: (err) => {
//           console.log(err);
//         },
//       });
//     },

//     //级联的西选择事件
//     hlhandleChange(value) {
//       console.log("el-cascader 选择的值:", value);
//       this.personal_information.RR_Province_Number = value[0];
//       this.personal_information.RR_City_Number = value[1];
//       this.personal_information.RR_County_Nubmer = value[2];
//       this.RR_Town_Number = value[3];
//       console.log(this.value);
//     },
//     //编辑进来方法
//     Edit(key) {
//       console.log(key);
//       let TABLE_NAME = sessionStorage.getItem("TABLE_NAME");
//       console.log(TABLE_NAME);
//       let rowkey = sessionStorage.getItem("rowkey");
//       console.log(rowkey);
//       const _this = this;
//       $.ajax({
//         headers: {
//           "content-type": "application/x-www-form-urlencoded",
//         },
//         url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
//         method: "post",
//         data: {
//           Data: TABLE_NAME, //表名party_fees表
//           Condition: `${key}='${rowkey}'`,
//         },
//         success: (res) => {
//           console.log(res);
//           _this.party_fees = res[0];
//           _this.value = [
//             res[0].RR_Province_Number,
//             res[0].RR_City_Number,
//             res[0].RR_County_Nubmer,
//             res[0].RR_Town_Number,
//           ];
//           //党员编号赋给dyhao
//           const dyhao = res[0].Party_Member_SerialNumber;
//           console.log(dyhao);
//           //调用查询党组织编号和党组名称
//           this.Add(dyhao);
//         },
//         error: (err) => {
//           console.log(err);
//         },
//       });
//     },
//     //提交表单
//     handleSubmit() {
//       //用李总结构多写一个这个
//       if(this.party_fees.Row_Status==""){
//         this.party_fees.Row_Status="0";
//       }
//       console.log(this.party_fees.Party_Organization_SerialNumber);
//       console.log(this.party_fees);//打印出来结构{ }

//       let type = sessionStorage.getItem("type");
//       console.log(type);
//       const appElement = document.getElementById("MainTable");
//       console.log(appElement);
//       let dataOptions = appElement.getAttribute("data-options");
//       console.log(dataOptions);
//       //动态获取到的表单数据
//       let options = JSON.parse(dataOptions);
//       console.log("options", options);

//       let lArray = new Array()  //创建一个数组
//       this.party_fees.Party_Member_SerialNumber.forEach(element => {  //forEach循环把Party_Member_SerialNumber的两个值拿出来， 生成对象传到数组a
//         console.log(element);
//         // 创建 this.party_fees 对象的浅拷贝  ，独立于原数据
//         let newPartyFees = {...this.party_fees };
//         console.log(this.party_fees);
//         newPartyFees.Party_Member_SerialNumber = element;     //把获得的element值（Party_Member_SerialNumber的值）赋给newPartyFees.Party_Member_SerialNumber
//         console.log(newPartyFees);         //{}结构
//         lArray.push(newPartyFees);         //传到数组a里  [{}]结构
//       });
//       console.log(lArray);    //[{},{}]
//       //动态获取的表名
//       let bm = options.TableName;
//       console.log(bm); //表名
//       let lJSONObject = new Object();
//       lJSONObject[bm] = lArray; //换表
//       console.log(lJSONObject);//获取到{party_fees: [Proxy(Object)]}结构
//       let lJSONObject_DataSet = new Object(); // Modify
//       if (type == "Edit") {
//         lJSONObject_DataSet.Modify = lJSONObject; //换表
//       } else {
//         lJSONObject_DataSet.Add = lJSONObject; //换表
//       }
//       this.AddApi(lJSONObject_DataSet);
//     },
//     AddApi(lJSONObject_DataSet) {
//       console.log(lJSONObject_DataSet);
//       const _this = this;
//       $.ajax({
//         url: `${mh}/clientOperation/generalSaveForClient`,
//         method: "post",
//         data: {
//           Data: JSON.stringify(lJSONObject_DataSet),
//         },
//         success: (res) => {
//           console.log(res);
//           // 在普通 JavaScript 中使用 MessageBox
//           ElMessage({
//             message: "提交成功",
//             type: "success",
//             plain: true,
//             duration: 1000, // 消息显示时长（毫秒）
//           });
//           // 等待 3 秒后回退页面
//           setTimeout(() => {
//             window.history.back();
//           }, 1000);
//         },
//         error: (err) => {
//           console.log(err);
//         },
//       });
//     },
//     goBack() {
//       window.history.back();
//     },
//   },
// };

const App = {
  mixins: [Save],//配置文件，路径/utils/mixins.js
  data() {
    return {
      category: [],
      Manager: [],
      dyManager: [],
      Party_Member_SerialNumber: [],
      checkAll: false,
      indeterminate: false,
      dataForm: {
        // Row_Status :'1'
      }
    };
  },
  mounted() {
    this.getCategory();
  },
  watch: {
    // 监听党组织变化
    'dataForm.Party_Organization_SerialNumber'(newVal) {
      if (newVal) {
        this.Party_Member_SerialNumber = []
        this.checkAll = false
        this.indeterminate = false
      }
    },
    // 监听已选值变化
    Party_Member_SerialNumber(val) {
      // 当没有可选项时，重置状态
      if (this.dyManager.length === 0) {
        this.checkAll = false;
        this.indeterminate = false;
        return;
      }
      // 当全部选中时
      if (val.length === this.dyManager.length) {
        this.checkAll = true
        this.indeterminate = false
      }
      // 当部分选中时
      else if (val.length > 0) {
        this.checkAll = false
        this.indeterminate = true
      }
      // 当未选中时
      else {
        this.checkAll = false
        this.indeterminate = false
      }
    }
  },
  methods: {
    handleCheckAll(val) {
      this.indeterminate = false
      if (val) {
        // 全选操作：提取所有选项的value
        this.Party_Member_SerialNumber = this.dyManager.map(
          item => item.Party_Member_SerialNumber
        )
      } else {
        // 取消全选
        this.Party_Member_SerialNumber = []
      }
    },
    getCategory() {
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
        type: "GET",
        data: {
          TableName: 'party_organization',
          NodeCode: "",
        },
        success: (data) => {
          this.Manager = data; // 直接使用 this
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });

    },
    handleTypeChange(val) {
      console.log(val);
      let selectedValue = val[val.length - 1]
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
        method: "post",
        data: {
          SQLSerialNumber: "f_party_member_information",
          Condition: '',
          ExtensionCondition: window.btoa(unescape(encodeURIComponent("AND party_member_information.Party_Organization_SerialNumber='" + selectedValue + "'")))
        },
        success: (res) => {
          if (this.action == 'Modify') {
            this.Party_Member_SerialNumber = [this.dataForm.Party_Member_SerialNumber];
          }
          this.dyManager = res;
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
      });
    },
    getData(nodeKey) {//查询
      const that = this;
      //获取对应子表表单对象
      let lJSONObject_Children = new Array();
      let lJSONObject_ChildData = new Object();
      try {
        //可能涉及多个子表
        document.getElementById("ChildTable").querySelectorAll(".el-descriptions").forEach(item => {
          let lJSONObject_ChildData_Options = new Object();
          lJSONObject_ChildData_Options = JSON.parse(item.getAttribute("data-options"));

          let ls_ChildTableName = lJSONObject_ChildData_Options.TableName;
          let ls_ChildDataForm = lJSONObject_ChildData_Options.form;

          lJSONObject_ChildData[ls_ChildTableName] = ls_ChildDataForm;
          lJSONObject_Children[ls_ChildTableName] = 'ChildTable';
        })
      } catch (error) { }
      //获得对应表单对象
      let lJSONObject_Data_Options = new Object();
      lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));
      let ls_DataForm = lJSONObject_Data_Options.form;
      let ls_TableName = lJSONObject_Data_Options.TableName;
      let ls_primary_key = lJSONObject_Data_Options.primary_key

      let lJSONObject_Data = new Object();
      lJSONObject_Data[ls_TableName] = ls_DataForm;
      lJSONObject_Data = { ...lJSONObject_Data, ...lJSONObject_ChildData };

      let lJSONObject_TableName = new Object();
      lJSONObject_TableName[ls_TableName] = 'MainTable'
      lJSONObject_TableName = { ...lJSONObject_TableName, ...lJSONObject_ChildData };
      for (let key in lJSONObject_TableName) {
        $.ajax({
          headers: requestHeaders(),
          url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
          method: "post",
          data: {
            Data: key,
            Condition: `${ls_primary_key}='${nodeKey}'`,
          },
          success: (res) => {
            if (lJSONObject_TableName[key] == 'MainTable') {
              this.handleTypeChange([res[0].Party_Organization_SerialNumber]);
              that[lJSONObject_Data[key]] = res[0];
            } else {
              that[lJSONObject_Data[key]] = res;
            }
          },
          complete: (XMLHttpRequest) => {
            requestPretreatment(XMLHttpRequest);
          },
        });
      }
    },
    async handleSubmit() {
      const allValid = await this.validateAllForms();
      if (allValid) {
        this.loading = true;
        let ls_URL = window.location.href;
        const action = getQueryObject(ls_URL, "action");   // 获取 action 参数
        let lJSONObject_Data_Options = new Object();
        lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));

        let ls_MainTableName = lJSONObject_Data_Options.TableName;
        let ls_DataForm = lJSONObject_Data_Options.form;
        let ls_primary_key = lJSONObject_Data_Options.primary_key;
        //没有子表下面内容就会报错，try/catch抛出异常
        try {
          //可能涉及多个子表
          document.getElementById("ChildTable").querySelectorAll(".el-descriptions").forEach(item => {
            let lJSONObject_ChildData_Options = new Object();
            lJSONObject_ChildData_Options = JSON.parse(item.getAttribute("data-options"));

            let ls_ChildTableName = lJSONObject_ChildData_Options.TableName;
            let ls_ChildDataForm = lJSONObject_ChildData_Options.form;
            // let ls_ParentTableName = lJSONObject_ChildData_Options.ParentTableName;
            if (this[ls_DataForm][ls_primary_key]) {
              this[ls_ChildDataForm].forEach(item => {
                item[ls_primary_key] = this[ls_DataForm][ls_primary_key];
              })
            }
            //建立子表结构体
            let JSONArray_ChildRecord = new Array();
            JSONArray_ChildRecord = this[ls_ChildDataForm];
            if (!this[ls_DataForm].Children) {
              this[ls_DataForm].Children = new Object();
            }
            this[ls_DataForm].Children[ls_ChildTableName] = JSONArray_ChildRecord;
          })
        } catch (error) {

        }
        //建立新增提交结构
        let lJSONObject_DataSet = new Object();
        if (action == 'Add') {
          if (this.nodeKey) {
            this[ls_DataForm].Parent_SerialNumber = this.nodeKey;
          }
          let lJSONArray_Record = new Array();
          this.Party_Member_SerialNumber.forEach(item => {
            lJSONArray_Record.push({ ...deepClone(this[ls_DataForm]), Party_Member_SerialNumber: item });
          })
          let lJSONObject_Table = new Object();
          lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;
          lJSONObject_DataSet[action] = lJSONObject_Table;
        } else if (action == 'Modify') {
          const { add, modify } = this.deal(this[ls_DataForm]);
          let lJSONArray_Record = new Array();
          lJSONArray_Record.push(modify);

          let lJSONObject_Table = new Object();
          lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;

          lJSONObject_DataSet[action] = lJSONObject_Table;
          if (add != null) {
            let lJSONArray_Record_Add = new Array();
            lJSONArray_Record_Add.push(add);

            let lJSONObject_Table_Add = new Object();
            lJSONObject_Table_Add[ls_MainTableName] = lJSONArray_Record_Add;

            lJSONObject_DataSet['Add'] = lJSONObject_Table_Add;
          }
        }
        let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
        if (!lJSONObject_DataSet['Add']) {
          lJSONObject_DataSet['Add'] = new Object();
        }
        lJSONObject_DataSet['Add']['base_operation_log'] = [{
          Operation_Type: '0',
          Operation_Path: this.relativePath,
          Operation_Main,
          Operation_Interface: "generalSave",
          Operation_IP: getIpAddress()
        }]
        submitSave(lJSONObject_DataSet, () => {
          if (res.Status == 'success') {
            // window.location.href = './index.html'
            window.history.go(-1);
          }
          this.loading = false;
        })
        // $.ajax({
        //   headers: requestHeaders(),
        //   url: `${mh}/generalBusiness/generalSave`,
        //   method: "post",
        //   data: {
        //     Data: JSON.stringify(lJSONObject_DataSet),
        //   },
        //   success: (res) => {
        //     if (res.Status == 'success') {
        //       // window.location.href = './index.html'
        //       window.history.go(-1);
        //     }
        //   },
        //   complete: (XMLHttpRequest) => {
        //     this.loading = false;
        //     requestPretreatment(XMLHttpRequest);
        //   },
        // });
      };
    },
  },
};


const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
app.component('yl-cascader', ylCascader)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
